SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE FUNCTION [Live].[GetIssuerByNAVDateOffset] (@RefNAVDate DATETIME, @DateOffset INT, @IssuerId int)
returns TABLE
AS RETURN
(
	SELECT top 1 i.* FROM FSFundModel.Reporting.Issuer i

	    
	CROSS APPLY	(
			SELECT * FROM Live.GetExportsByNAVDateOffset(@DateOffset, default) tvf
			WHERE tvf.BaseNAVDate = @RefNAVDate
	 ) ca
	WHERE i.ExportId = ca.OffsetExportId
	AND i.IssuerId = @IssuerId
	--ExportId = (SELECT MAX(ExportId) FROM FSFundModel.Reporting.Export WHERE NAVDate = @NAVDate AND FundId = @FundId)
)
GO
EXEC sp_addextendedproperty N'VirtualFolder', N'Live', 'SCHEMA', N'Live', 'FUNCTION', N'GetIssuerByNAVDateOffset', NULL, NULL
GO
